package leetcode.editor.cn;

import utils.TreeNode;

/**
 * 找树左下角的值
 * @author qulq
 * @date 2024-07-07 11:24:38
 */
public class T513_FindBottomLeftTreeValue{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T513_FindBottomLeftTreeValue().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

	int maxDepth = Integer.MIN_VALUE;
	int ans;

    public int findBottomLeftValue(TreeNode root) {
		ans = 0;
		recursion(root, 0);
		return ans;
    }

	private void recursion(TreeNode root, int depth) {
		if (root.left == null && root.right == null) {
			if (depth > maxDepth) {
				ans = root.val;
				maxDepth = depth;
				return;
			}
		}
		if (root.left != null) {
			recursion(root.left, depth + 1);
		}
		if (root.right != null) {
			recursion(root.right, depth + 1);
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}